from sqlalchemy import create_engine, Column, Integer, String, DateTime, Enum, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
import sqlalchemy


Base = declarative_base()
class Logger(Base):
    __tablename__ = 'logger'
    
    id = Column(Integer,autoincrement=True, primary_key=True)
    vin = Column('VIN',String(17))
    time = Column('创建时间',DateTime, default=datetime.now)
    status = Column('状态',Enum('成功', '失败'))
    log = Column('日志',String(150))


def create_db_sqlite3():
    
    engine = create_engine('sqlite:///mydatabase.sqlite3')
    
    # 创建表格
    Base.metadata.create_all(bind=engine)


def create_db_mysql():
    # 连接到数据库
    engine = create_engine('mysql://admin_00001:admin_00001@127.0.0.1:3306/sqlalchemy_20240226', echo=True)
    # 创建表格
    Base.metadata.create_all(bind=engine)



# create_db_sqlite3()

# create_db_mysql()

def main01():
    engine = create_engine('mysql://admin_00001:admin_00001@127.0.0.1:3306/sqlalchemy_20240226', echo=True)
    # 创建DBSession类型:
    DBSession = sessionmaker(bind=engine)


    Base = declarative_base()

    # 定义User对象:
    class User(Base):
        __tablename__ = 'users'
        
        id = Column(Integer, primary_key=True, index=True, autoincrement=True)
        create_time = Column(DateTime, default=datetime.now)
        name = Column(String(50))
        age = Column(Integer)


    # 建立一个新的User对象:
    new_user = User( name='Bob', age=20)

    # 创建session对象:
    session = DBSession()
    session.add(new_user)
    session.commit()
    session.close()




    # create_db_mysql()